package com.sgb.kjwl.view.ui.activity.manage;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityWebsiteMyjjBinding;
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageComponent;
import com.sgb.kjwl.di.manage.EnterpriseManageModule;
import com.sgb.kjwl.model.entity.manage.WebsiteInfoEntity;
import com.sgb.kjwl.viewmodel.manage.WebsiteMyXiXinModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.SharedPreferenceUtil;

import javax.inject.Inject;

/**
 * @author 张驰
 * @date 2021/6/3 18:01
 * @Description 个人网站发布板块数据展示界面
 * @name com.sgb.kjwl.view.ui.activity.manage
 */
public class WebsiteMyXiXinActivity extends AppActivity {

    private int REQUEST_CODE = 1000;

    @Inject
    WebsiteMyXiXinModel viewModel;
    private ActivityWebsiteMyjjBinding binding;
    private WebsiteInfoEntity websiteMyInfoEntity;
    private RequestManager requestManager;

    public static void start(Context context) {
        Intent intent = new Intent(context, WebsiteMyXiXinActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerEnterpriseManageComponent.builder().appComponent(appComponent).
                enterpriseManageModule(new EnterpriseManageModule(this)).
                build().inject(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_website_myjj);
        requestManager = Glide.with(this);
        binding.include.titleView.setText("基本信息");
        binding.tvUpdate.setOnClickListener(this);
        binding.include.ivBack.setOnClickListener(this);
        initRefreshView();
        viewModel.getWebsiteInfoData().observe(this, new Observer<WebsiteInfoEntity>() {

            @Override
            public void onChanged(WebsiteInfoEntity myEntity) {
                websiteMyInfoEntity = myEntity;
                setAvatarView(myEntity.getLogo());
                if (!TextUtils.isEmpty(websiteMyInfoEntity.getMainBusiness())){
                    binding.comServiceTypeDetail.setText(websiteMyInfoEntity.getNewMainBusinessName());
                }else {
                    binding.comServiceTypeDetail.setText("-");
                }
                binding.setViewModel(myEntity);
            }
        });
        binding.itvUserName.setContent(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.REAL_NAME, ""));
    }

    /**
     * 显示用户图像
     * @param logo
     */
    private void setAvatarView(String logo) {
        if (!TextUtils.isEmpty(logo)) {
            RequestOptions options = new RequestOptions()
                    .placeholder(R.mipmap.image_website_dfg)//图片加载出来前，显示的图片
                    .error(R.mipmap.image_website_dfg)//图片加载失败后，显示的图片
                    .transform(new CircleCrop());
            requestManager.load(logo).apply(options).into(binding.ivShopAvatar);
        }
    }

    private void initRefreshView() {
        binding.swipeRefresh.setEnableLoadMore(false);
        // 刷新
        binding.swipeRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                init();
                binding.swipeRefresh.finishRefresh(500);
            }
        });
    }

    @Override
    protected void init() {
        //网店编号
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, "");
        viewModel.getWebsiteInfo(this,companyNo,"0");
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_update:
                if (!ClickUtils.isDoubleClick()) {
                    WebsiteUpDataMyXiXinActivity.start(this,websiteMyInfoEntity,REQUEST_CODE);
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
            init();
//            if (data != null && data.getSerializableExtra("Data") != null){
//                websiteMyInfoEntity = (WebsiteInfoEntity)data.getSerializableExtra("Data");
//                binding.setViewModel(websiteMyInfoEntity);
//                if (!TextUtils.isEmpty(websiteMyInfoEntity.getNewMainBusinessName())){
//                    binding.comServiceTypeDetail.setText(websiteMyInfoEntity.getNewMainBusinessName());
//                }else {
//                    binding.comServiceTypeDetail.setText("-");
//                }
//            }
        }
    }
}
